﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PassBag.Data;
using PassBag.Logic;
using PassBag.Logic.Settings;

namespace PassBag.Presentation {
    public partial class EditPasswordDialog : Form {
        private PasswordObject _passwordObject;

        public PasswordObject PasswordObject {
            get {
                return _passwordObject;
            }
        }
        private bool _editMode = false;

        public bool EditMode {
            get {
                return _editMode;
            }
        }

        public EditPasswordDialog () {
            InitializeComponent ();
            _passwordObject = new PasswordObject ();

            this._editMode = false;

            LoadRessource ();

        }

        public EditPasswordDialog (PasswordObject passwordObject) {
            InitializeComponent ();
            this._editMode = true;

            LoadRessource ();

            this._passwordObject = passwordObject;
            LoadPasswordItem (); // load information
        }

        #region Load Resource

        /// <summary>
        /// Loads the ressource.
        /// </summary>
        private void LoadRessource () {
            if (this._editMode) {
                this.Text = Properties.Resources.EditPasswordDialog_TextEdit;                
            }
            else {
                this.Text = Properties.Resources.EditPasswordDialog_TextAdd;
            }

            dialogTitleLabel.Text = Properties.Resources.EditPasswordDialog_Title;
            label2.Text = Properties.Resources.EditPasswordDialog_TitleText;
            label3.Text = Properties.Resources.EditPasswordDialog_UserText;
            label4.Text = Properties.Resources.EditPasswordDialog_PasswordText;
            label5.Text = Properties.Resources.EditPasswordDialog_ConfirmText;
            label6.Text = Properties.Resources.EditPasswordDialog_UrlText;
            label7.Text = Properties.Resources.EditPasswordDialog_CommentText;

            generatePasswordButton.Text = Properties.Resources.EditPasswordDialog_GeneratePassBtn;
            showPasswordButton.Text = Properties.Resources.EditPasswordDialog_ShowPassBtn;

            this.okBtn.Text = Properties.Resources.Global_OkButton;
            this.cancelBtn.Text = Properties.Resources.Global_CancelButton;
            
        }

        #endregion


        private void LoadPasswordItem () {
            titleTextBox.Text = _passwordObject.Title;
            usernameTextBox.Text = _passwordObject.UserName;
            passwordTextBox.Text = _passwordObject.Password;
            confirmPasswordTextBox.Text = _passwordObject.Password;
            urlTextBox.Text = _passwordObject.Url;
            commentTextBox.Text = _passwordObject.Comment;
        }

        private void okBtn_Click (object sender, EventArgs e) {

            if (string.IsNullOrEmpty(usernameTextBox.Text)) {
                MessageDialog.ShowError (this, Properties.Resources.EditPasswordDialog_MsgUserEmpty);
            }
            else if (string.IsNullOrEmpty (passwordTextBox.Text) || string.IsNullOrEmpty (confirmPasswordTextBox.Text)) {
                MessageDialog.ShowError (this, Properties.Resources.EditPasswordDialog_MsgFillPassAndConf);
            }
            else if (! passwordTextBox.Text.Equals (confirmPasswordTextBox.Text)) {
                MessageDialog.ShowError (this, Properties.Resources.EditPasswordDialog_MsgPasNotMatch);
            }
            else { // Save or insert password class
                if (! _editMode) {
                    _passwordObject.Id = Guid.NewGuid ();  // If new create new Id
                    _passwordObject.Information = "";
                }

                _passwordObject.Title = titleTextBox.Text;
                _passwordObject.UserName = usernameTextBox.Text;
                _passwordObject.Password = passwordTextBox.Text;
                _passwordObject.Url = urlTextBox.Text;
                _passwordObject.Comment = commentTextBox.Text;
                _passwordObject.LastModified = DateTime.Now;


                DialogResult = DialogResult.OK;
                Close ();
            }
        }

        private void generatePasswordButton_Click (object sender, EventArgs e) {
            PasswordPolicy passwordPolicy = new PasswordPolicy (XmlSettingsManager.Settings.PassPolicyNumberOfDigit,
                                                          XmlSettingsManager.Settings.PassPolicyNumberOfSpecialChars,
                                                          XmlSettingsManager.Settings.PassPolicyMaxLength);
            string password = passwordPolicy.GeneratePassword ();
            passwordTextBox.Text = password;
            confirmPasswordTextBox.Text = password;

        }

        private void showPasswordButton_Click (object sender, EventArgs e) {
            // if timer enabled do nothing
            if (! passwordTimer.Enabled) {
                passwordTextBox.UseSystemPasswordChar = false;
                passwordTimer.Enabled = true;
            }
            
        }

        private void passwordTimer_Tick (object sender, EventArgs e) {
            passwordTimer.Enabled = false;
            passwordTextBox.UseSystemPasswordChar = true;
        }
    }
}
